package github.kawaiior.yuanchuxiuzhen.client.key;

import github.kawaiior.yuanchuxiuzhen.Yuanchuxiuzhen;
import github.kawaiior.yuanchuxiuzhen.client.gui.panel.OpenPanelGUI;
import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.core.registry.ModCapability;
import github.kawaiior.yuanchuxiuzhen.network.NetworkRegistryHandler;
import github.kawaiior.yuanchuxiuzhen.network.packet.UpdateManaPacket;
import net.minecraft.client.Minecraft;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.client.util.InputMappings;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.client.settings.KeyConflictContext;
import net.minecraftforge.client.settings.KeyModifier;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.common.Mod;
import org.lwjgl.glfw.GLFW;

@Mod.EventBusSubscriber(value = Dist.CLIENT)
public class KeyBoardInput {
    // 原初修真面板
    public static final KeyBinding YCXZ_PANEL_KEY = new KeyBinding("key.panel",
            KeyConflictContext.IN_GAME,
            KeyModifier.CONTROL,
            InputMappings.Type.KEYSYM,
            GLFW.GLFW_KEY_J,
            "key.category." + Yuanchuxiuzhen.MODID);

    // 灵根以及功法
    public static final KeyBinding YCXZ_GONG_FA = new KeyBinding("key.gongfa",
            KeyConflictContext.IN_GAME,
            InputMappings.Type.KEYSYM,
            GLFW.GLFW_KEY_K,
            "key.category." + Yuanchuxiuzhen.MODID);

    @SubscribeEvent
    public static void onKeyboardInput(InputEvent.KeyInputEvent event) {
        if (YCXZ_PANEL_KEY.isPressed()) {
            assert Minecraft.getInstance().player != null;
            PlayerEntity player = Minecraft.getInstance().player;
            NetworkRegistryHandler.INSTANCE.sendToServer(new UpdateManaPacket());
            if (player.world.isRemote){
                DistExecutor.safeCallWhenOn(Dist.CLIENT, () -> OpenPanelGUI::new);
            }
        }
        else if (YCXZ_GONG_FA.isPressed()){
            assert Minecraft.getInstance().player != null;
            PlayerEntity player = Minecraft.getInstance().player;
            NetworkRegistryHandler.INSTANCE.sendToServer(new UpdateManaPacket());
            LazyOptional<ManaPower> capability = player.getCapability(ModCapability.MANA_POWER);
            capability.ifPresent((power)->{
                player.sendMessage(new StringTextComponent("当前灵根为: "+power.getManaRootText()), player.getUniqueID());
            });
        }
    }
}
